import java.io.IOException;
import java.net.*;

import net.sourceforge.rconed.SourceRcon;
import net.sourceforge.rconed.exception.*;
public class ServerConnection extends SourceRcon
{
	private InetAddress ip;
	private int port;
	private String myip;
	private int logport;
	private String rcon;
	private DatagramSocket log;
	ServerConnection (String ip,int port,String myip,int logport,String rcon) throws IOException {
		this.ip = InetAddress.getByName(ip);
		this.port = port;
		this.myip = myip;
		this.logport = logport;
		this.rcon = rcon;
		this.log = new DatagramSocket(logport);
	}
	void active () throws SocketTimeoutException, BadRcon, ResponseEmpty, IOException {
		send("log on");
		send("logaddress_add " + this.myip + ":" + this.logport);
		this.log.connect(this.ip,this.port);
		new Thread(
				new Runnable(){
					public void run(){
						String read;
						DatagramPacket readpacket = new DatagramPacket(new byte[8192],8192);
						DatagramSocket log = getlog();
						while(true){
							readpacket = new DatagramPacket(new byte[8192],8192);
							try {
								log.receive(readpacket);
							} catch (IOException e) {
								e.printStackTrace();
							}
							read = new String(readpacket.getData());
							read = read.substring(30);
							System.out.println(read);
						}
					}
				}
		).start();
	}
	DatagramSocket getlog() { return this.log; }
	String send (String cmd) throws SocketTimeoutException, BadRcon, ResponseEmpty, IOException { return send(this.ip.getHostAddress(),this.port,this.rcon,cmd); }
}

